package io.virtualapp.widgets;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import io.virtualapp.R;


/**
 * Created by:zeoy
 * 日期:2018/1/9
 * 介绍: 居中的dialog
 */

public class CenterDialog extends AlertDialog implements View.OnClickListener {
    private  int[] listenedItems;
    private  int layoutResID;
    private  Context context;
    private OnCenterItemClickListener listener;

    public interface OnCenterItemClickListener {
        void OnCenterItemClick(CenterDialog dialog, View view);
    }

    public CenterDialog(@NonNull Context context) {
        super(context);
    }

    public CenterDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CenterDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }
    public CenterDialog(Context context, int layoutResID, int[] listenedItems) {
        super(context, R.style.dialog_custom); //dialog的样式
        //super(context);
        this.context = context;
        this.layoutResID = layoutResID;
        this.listenedItems = listenedItems;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置为居中
        //window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
        setContentView(layoutResID);

        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth()*4/5; // 设置dialog宽度为屏幕的4/5
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);// 点击Dialog外部消失
        //遍历控件id,添加点击事件
        if (listenedItems!=null)
        for (int id : listenedItems) {
            findViewById(id).setOnClickListener(this);
        }
    }

    @Override
    public void show() {
        super.show();

    }

    @Override
    public void onClick(View v) {

        if (listener!=null){
            listener.OnCenterItemClick(this,v);
        }
        dismiss();
    }

    public void setOnCenterClickListener(OnCenterItemClickListener listener) {
        this.listener = listener;
    }

    /**
     * 根据资源id设置数据
     * @param layoutResID
     * @param s
     */
    public void setText(int layoutResID,String s){
        TextView textView = (TextView)findViewById(layoutResID);
        if (textView!=null){
            textView.setText(s);
        }
    }

    /**
     * 对于带list的dialog初始化
     * @param listId
     * @param adapter
     * @param <T>
     */
    public<T extends BaseAdapter> void initListView(int listId, T adapter, OnDialogItemClickListener listener){
        ListView list = findViewById(listId);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listener.onItemClick(position,id);
            }
        });
    }

    /**
     * Created by Administrator on 2018\1\26 0026.
     * 带listview的dialog实现回调监听
     */

    public interface OnDialogItemClickListener {
        public void onItemClick(int position, long id);
    }
}
